package com.cocotingo.snail.samples;

import java.awt.Color;
import java.util.ArrayList;

import javax.swing.JFrame;

import com.cocotingo.snail.MouseEvent;
import com.cocotingo.snail.Vector;
import com.cocotingo.snail.View;
import com.cocotingo.snail.ViewContext;
import com.cocotingo.snail.text.GroupedRichTextView;
import com.cocotingo.snail.text.RichTextBlock;
import com.cocotingo.snail.text.RichTextParagraph;
import com.cocotingo.snail.text.RichTextView;
import com.cocotingo.snail.text.TextView;

public class SampleMain2 {

	public static void main(String[] args) {
		JFrame frame = new JFrame("DocStack GUI Fundation Sample");
		frame.setBounds(200, 200, 640, 480);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		final View rootView = new ViewContext(frame, new View(0, 0, 0, 0) {
		}).getRootView();
		
		final TextView textView = new TextView(200, 200, 50, 50) {
			protected void postMouseClicked(MouseEvent e) {
				RichTextView view = (RichTextView) getSuperView().getTaggedSubview(1);
				view.setAlpha(view.getAlpha() * 0.75f);
			}
		};
		
		textView.setText("Yes, you are right!");
		textView.setBackgroundColor(Color.lightGray);
		textView.setSize(textView.getPreferredSize());
		rootView.addSubview(textView);
		
		final GroupedRichTextView textView2 = new GroupedRichTextView(10, 10, 200, 200) {
			Vector pos;
			Vector size;
			@Override
			protected void postMousePressed(MouseEvent e) {
				pos = e.getPosition(this);
				size = getSize();
			}
			@Override
			protected void postMouseDragged(MouseEvent e) {
				Vector new_pos = e.getPosition(this);
				setSize(size.add(new_pos.add(pos.getInversedVector())));
				setHeight(getPreferredHeight());
			}
		};
		
		textView2.setBackgroundColor(Color.lightGray);
		textView2.setTag(1);
		
		ArrayList<RichTextBlock> blocks = new ArrayList<RichTextBlock>();
		blocks.add(new RichTextBlock("Hello, World! This is a very long passage to test the auto line-breaker method. I think the result is very good. Hahaha~"));
		blocks.add(new RichTextBlock("WWW.").setColor(Color.red));
		blocks.add(new RichTextBlock("COCOTINGO").setColor(Color.yellow));
		blocks.add(new RichTextBlock(".COM").setColor(Color.blue));
		blocks.add(new RichTextBlock("This is very good and i like it. Not long enough long enough long enough!!").setColor(Color.green).setHotspot(true));
		
		final ArrayList<RichTextParagraph> paragraphs = new ArrayList<RichTextParagraph>();
		paragraphs.add(new RichTextParagraph(blocks.toArray(new RichTextBlock[0])).setMargin(10, 10, 10, 10));
		paragraphs.add(new RichTextParagraph(blocks.toArray(new RichTextBlock[0])).setMargin(10, 0, 10, 10));
		paragraphs.get(0).setLinebreakMode(RichTextView.LINEBREAK_MODE_DISABLE);
		
		textView2.setText(paragraphs.toArray(new RichTextParagraph[0]));
		rootView.addSubview(textView2);
		
		final RichTextView textView3 = new RichTextView(200, 200, 300, 300);
		textView3.setBackgroundColor(Color.gray);
		rootView.addSubview(textView3);
		
		View button = new View(10, 10, 20, 20) {
			{
				setBackgroundColor(Color.black);
			}
			boolean flag = false;
			@Override
			protected void postMouseClicked(MouseEvent e) {
				if (flag) {
					textView2.setText(paragraphs.toArray(new RichTextParagraph[0]));
				} else {
					RichTextParagraph paragraph = new RichTextParagraph(new RichTextBlock[] {
						new RichTextBlock("Hello, World! This is a very long passage to test the auto line-breaker method. I think the result is very good. Hahaha~")
					});
					textView2.setText(new RichTextParagraph[] {paragraph.setMargin(10, 10, 10, 10)});
				}
				flag = !flag;
				textView.setText("Test text paint.");
				
				textView3.setText(new RichTextBlock[] {
					new RichTextBlock("Hello, World! This is a very long passage to test the auto line-breaker method. I think the result is very good. Hahaha~")
				});
				textView3.removeFromSuperView();
				rootView.addSubview(textView3);
				
			}
		};
		rootView.addSubview(button);
		
		frame.setVisible(true);
	}

}
